package es.udc.emarketapp.emarket.model.purchase.purchaseFacade;

import es.udc.emarketapp.emarket.model.order.vo.OrderVO;
import es.udc.emarketapp.emarket.model.orderDetails.vo.OrderDetailsVO;
import es.udc.emarketapp.emarket.model.orderLine.vo.OrderLineVO;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.CreateOrderAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.CreateOrderLineAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.CreatePurchaseAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.DeleteOrderAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.DeleteOrderLineAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.DeletePurchaseAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindAllPurchasesAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindLastPurchaseByClientAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrderByIdAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrderDetailsByIdAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrderLineByIdAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrderLinesByOrderAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrdersByPurchaseAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrdersDetailsBySellerAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrdersResumeByDateAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrdersResumeByPaidPendentAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrdersResumeByPaidPendentAndSellerAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrdersResumeBySellerAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrdersResumeByShippingPendentAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindOrdersResumeByShippingPendentAndSellerAction;

import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindPurchaseByIdAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.FindPurchasesByClientAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.ModifyOrderAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.ModifyOrderLineAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.ModifyPurchaseAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.UpdatePaymentDateAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.UpdatePurchaseValueAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions.UpdateShippingDateAction;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.vo.OrderChunkVO;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.vo.OrderLineChunkVO;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.vo.OrderResumeChunkVO;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.vo.PurchaseChunkVO;
import es.udc.emarketapp.emarket.model.purchase.vo.PurchaseVO;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.exceptions.ModelException;
import es.udc.fbellas.j2ee.util.sql.PlainActionProcessor;
import es.udc.fbellas.j2ee.util.sql.SimpleDataSource;
import java.util.Calendar;
import javax.sql.DataSource;

public class PurchaseFacade {

    private DataSource dataSource;

    public PurchaseFacade() throws InternalErrorException {

        dataSource = new SimpleDataSource();

    }

    /***
     * PURCHASE
     **/
    public PurchaseVO createPurchase(PurchaseVO purchaseVO)
            throws InternalErrorException {

        try {

            CreatePurchaseAction action = new CreatePurchaseAction(purchaseVO);

            return (PurchaseVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // createPurchase

    public PurchaseVO findPurchaseById(Long idPurchase)
            throws InternalErrorException, InstanceNotFoundException {

        try {

            FindPurchaseByIdAction action = new FindPurchaseByIdAction(
                    idPurchase);

            return (PurchaseVO) PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // findPurchaseById
    
     public PurchaseVO findLastPurchaseByClient(String idClient)
            throws InternalErrorException, InstanceNotFoundException {

        try {

            FindLastPurchaseByClientAction action = new FindLastPurchaseByClientAction(
                    idClient);

            return (PurchaseVO) PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // findLastPurchaseByClient


    public PurchaseChunkVO findPurchasesByClient(String idClient,
            int startIndex, int count) throws InternalErrorException {

        try {

            FindPurchasesByClientAction action = new FindPurchasesByClientAction(
                    idClient, startIndex, count);

            return (PurchaseChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }

    } // findPurchasesByClient

    public PurchaseChunkVO findAllPurchases(int startIndex, int count)
            throws InternalErrorException {

        try {

            FindAllPurchasesAction action = new FindAllPurchasesAction(
                    startIndex, count);

            return (PurchaseChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }

    } // findAllPurchases

    public void modifyPurchase(PurchaseVO purchaseVO)
            throws InstanceNotFoundException, InternalErrorException {

        try {

            ModifyPurchaseAction action = new ModifyPurchaseAction(purchaseVO);
            PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // modifyPurchase

    public void updatePurchaseValue(Long nPurchase, double value)
            throws InstanceNotFoundException, InternalErrorException {

        try {

            UpdatePurchaseValueAction action =
                    new UpdatePurchaseValueAction(nPurchase, value);
            PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // updatePurchaseValue

    public void deletePurchase(Long idPurchase)
            throws InstanceNotFoundException, InternalErrorException {

        try {

            DeletePurchaseAction action = new DeletePurchaseAction(idPurchase);
            PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // deletePurchase

    /***
     * ORDER
     **/
    public OrderDetailsVO createOrder(OrderDetailsVO orderVO) throws InternalErrorException {

        try {

            CreateOrderAction action = new CreateOrderAction(orderVO);

            return (OrderDetailsVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // createOrder

    public OrderVO findOrderById(Long idOrder)
            throws InternalErrorException {

        try {

            FindOrderByIdAction action = new FindOrderByIdAction(idOrder);

            return (OrderVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // findOrderById

    public OrderDetailsVO findOrderDetailsById(Long idOrder)
            throws InternalErrorException {

        try {

            FindOrderDetailsByIdAction action =
                    new FindOrderDetailsByIdAction(idOrder);

            return (OrderDetailsVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // findOrderDetailsById

    public OrderChunkVO findOrdersByPurchase(Long idPurchase, int startIndex,
            int count) throws InternalErrorException {

        try {

            FindOrdersByPurchaseAction action = new FindOrdersByPurchaseAction(
                    idPurchase, startIndex, count);

            return (OrderChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }

    } // findOrdersBySeller

    public OrderChunkVO findOrdersBySeller(String idSeller, int startIndex,
            int count) throws InternalErrorException {

        try {

            FindOrdersDetailsBySellerAction action = new FindOrdersDetailsBySellerAction(
                    idSeller, startIndex, count);

            return (OrderChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }

    } // findOrdersBySeller




    public OrderResumeChunkVO findOrdersResumeBySeller(String idSeller, int startIndex,
            int count) throws InternalErrorException {

        try {

            FindOrdersResumeBySellerAction action = new FindOrdersResumeBySellerAction(
                    idSeller, startIndex, count);

            return (OrderResumeChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }

    } // findOrdersResumeBySeller


   public OrderResumeChunkVO findOrdersByDate(Calendar startDate, Calendar endDate,
        int startIndex, int count) throws InternalErrorException {

        try {

            FindOrdersResumeByDateAction action =
                new  FindOrdersResumeByDateAction(startDate, endDate, startIndex, count);

            return (OrderResumeChunkVO) PlainActionProcessor.process(
                dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    }

    public OrderResumeChunkVO findOrdersResumeByPaidPendent(int startIndex,
            int count) throws InternalErrorException {

        try {

            FindOrdersResumeByPaidPendentAction action =
                    new FindOrdersResumeByPaidPendentAction(startIndex, count);

            return (OrderResumeChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }

    } // findOrdersResumeByPaidPendent

    public OrderResumeChunkVO findOrdersResumeByPaidPendentAndSeller(String idSeller, int startIndex,
            int count) throws InternalErrorException {

        try {

            FindOrdersResumeByPaidPendentAndSellerAction action =
                    new FindOrdersResumeByPaidPendentAndSellerAction(
                    idSeller, startIndex, count);

            return (OrderResumeChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }

    } // findOrdersResumeByPaidPendentAndSeller

    public OrderResumeChunkVO findOrdersResumeByShippingPendent(int startIndex,
            int count) throws InternalErrorException {

        try {

            FindOrdersResumeByShippingPendentAction action =
                    new FindOrdersResumeByShippingPendentAction(startIndex, count);

            return (OrderResumeChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }

    } // findOrdersResumeByShippingPendent

    public OrderResumeChunkVO findOrdersResumeByShippingPendentAndSeller(String idSeller, int startIndex,
            int count) throws InternalErrorException {

        try {

            FindOrdersResumeByShippingPendentAndSellerAction action =
                    new FindOrdersResumeByShippingPendentAndSellerAction(
                    idSeller, startIndex, count);

            return (OrderResumeChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }

    } // findOrdersResumeByShippingPendentAndSeller

    public void updateShippingDate(Long nOrder) throws InstanceNotFoundException,
            InternalErrorException {

        try {
            UpdateShippingDateAction action = new UpdateShippingDateAction(nOrder);
            PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } //updateShippingDate

    public void updatePaymentDate(Long nOrder) throws InstanceNotFoundException,
            InternalErrorException {

        try {
            UpdatePaymentDateAction action = new UpdatePaymentDateAction(nOrder);
            PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } //updatePaymentDate

    public void modifyOrder(OrderDetailsVO orderVO) throws InstanceNotFoundException,
            InternalErrorException {

        try {

            ModifyOrderAction action = new ModifyOrderAction(orderVO);
            PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // modifyOrder

    public void deleteOrder(Long idOrder, Long idPurchase)
            throws InstanceNotFoundException, InternalErrorException {

        try {

            DeleteOrderAction action = new DeleteOrderAction(idOrder,
                    idPurchase);
            PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // deleteOrder

    /***
     * ORDER LINE
     **/
    public OrderLineVO createOrderLine(OrderLineVO orderLineVO)
            throws InternalErrorException {

        try {

            CreateOrderLineAction action = new CreateOrderLineAction(
                    orderLineVO);

            return (OrderLineVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // createLine

    public OrderLineVO findOrderLineById(Long idOrderLine,
            Long idOrder, Long idPurchase)
            throws InternalErrorException {

        try {

            FindOrderLineByIdAction action =
                    new FindOrderLineByIdAction(idOrderLine, idOrder, idPurchase);

            return (OrderLineVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // findOrderLineById

    public OrderLineChunkVO findOrderLinesByOrder(Long idOrder,
            Long idPurchase, int startIndex, int count)
            throws InternalErrorException {

        try {

            FindOrderLinesByOrderAction action = new FindOrderLinesByOrderAction(
                    idOrder, idPurchase, startIndex, count);

            return (OrderLineChunkVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }

    } // findOrderLinesByOrder

    public void modifyOrderLine(OrderLineVO orderLineVO)
            throws InstanceNotFoundException, InternalErrorException {

        try {

            ModifyOrderLineAction action =
                    new ModifyOrderLineAction(orderLineVO);
            PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // modifyOrderLine

    public void deleteOrderLine(Long idOrderLine,
            Long idOrder, Long idPurchase)
            throws InstanceNotFoundException, InternalErrorException {

        try {

            DeleteOrderLineAction action =
                    new DeleteOrderLineAction(idOrderLine, idOrder, idPurchase);
            PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }

    } // deleteOrderLine
} // class

